package com.shop2cn.iapi.couponmanage.testcase;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.couponmanage.parameter.old.BatchInfo;
import com.shop2cn.iapi.couponmanage.parameter.CreateBean;
import com.shop2cn.iapi.couponmanage.parameter.CreateCouponBean;
import com.shop2cn.iapi.couponmanage.parameter.GetBatchInfoBean;
import com.shop2cn.iapi.couponmanage.parameter.SellerCreateBatchBean;
import com.shop2cn.iapi.couponmanage.parameter.executeTaskBean;
import com.shop2cn.iapi.couponmanage.service.CreateCall;
import com.shop2cn.iapi.couponmanage.service.CreateCouponCall;
import com.shop2cn.iapi.couponmanage.service.GetBatchInfoCall;
import com.shop2cn.iapi.couponmanage.service.SellerCreateBatchCall;
import com.shop2cn.iapi.couponmanage.service.executeTaskCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponShop2cnMCallService;
import com.ymttest.database.model.CouponShop2cn.couponbatch;
import com.ymttest.database.model.CouponShop2cn.couponusecondition;
import com.ymttest.database.sqlwapper.CouponShop2cn.couponbatchWapper;
import com.ymttest.database.sqlwapper.CouponShop2cn.couponsendsubtaskWapper;
import com.ymttest.database.sqlwapper.CouponShop2cn.couponuseconditionWapper;
import com.ymttest.utils.TestDataManager;
//import com.ymttest.database.model.YmtCouponBatch;
//import com.ymttest.database.sqlwapper.couponbatchWapper;
/**
*获取批次信息用例
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetBatchInfo{
	private static GetBatchInfoBean getbatchinfoBean;
	private static GetBatchInfoCall getbatchinfoCall; 
	private SellerCreateBatchBean sellercreatebatchBean =new SellerCreateBatchBean();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取批次信息用例");
	}

	@Before
	public void caseUp() {
		getbatchinfoBean=new GetBatchInfoBean();
		getbatchinfoCall=new GetBatchInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getbatchinfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBatchInfo_001() {
		Logger.start(true,"获取批次信息-商家优惠券-优惠券适用类型:1-全店通用");
		try {
			CouponShop2cnMCallService CouponShop2cnMCallService = new CouponShop2cnMCallService();
			sellercreatebatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			sellercreatebatchBean.setUseConditionType(1);
			SellerCreateBatchCall sellerCreateBatchCall = CouponShop2cnMCallService.SellerCreateBatch(sellercreatebatchBean);
			List<couponbatch> list = new couponbatchWapper().selectBybatchCode(sellerCreateBatchCall.getBatchCode());
			
			getbatchinfoBean.setBatchCode(list.get(0).getBatchCode().toString());
			getbatchinfoCall.setData(getbatchinfoBean);
			getbatchinfoCall.callService();			
			
			VerifyReturn(list.get(0));
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBatchInfo_002() {
		Logger.start(true,"获取批次信息-商家优惠券-优惠券适用类型:2- 指定商品");
		try {
			CouponShop2cnMCallService CouponShop2cnMCallService = new CouponShop2cnMCallService();
			sellercreatebatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			sellercreatebatchBean.setUseConditionType(2);
			List<String> pIdStrings=new ArrayList();
			pIdStrings.add("p"+TestDataManager.getRandomIntNum(5));
			pIdStrings.add("p"+TestDataManager.getRandomIntNum(5));
			sellercreatebatchBean.setproductIdList(pIdStrings);
			SellerCreateBatchCall sellerCreateBatchCall = CouponShop2cnMCallService.SellerCreateBatch(sellercreatebatchBean);
			List<couponbatch> list = new couponbatchWapper().selectBybatchCode(sellerCreateBatchCall.getBatchCode());

			getbatchinfoBean.setBatchCode(list.get(0).getBatchCode().toString());
			getbatchinfoCall.setData(getbatchinfoBean);
			getbatchinfoCall.callService();			
			
			VerifyReturn(list.get(0));
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBatchInfo_003() {
		Logger.start(true,"获取批次信息-商家优惠券");
		try {
			List<couponbatch> list = new couponbatchWapper().getTop1SellerBatchInfo();

			getbatchinfoBean.setBatchCode(list.get(0).getBatchCode().toString());
			getbatchinfoCall.setData(getbatchinfoBean);
			getbatchinfoCall.callService();			
			
			VerifyReturn(list.get(0));
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_GetBatchInfo_006() {
		Logger.start(true,"获取批次信息-商家优惠券-分享券");
		try {
			// 分享券	
			CouponShop2cnMCallService CouponShop2cnMCallService = new CouponShop2cnMCallService();
			sellercreatebatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			
			sellercreatebatchBean.setCouponType((byte)2);
			sellercreatebatchBean.setEffectiveDays(5);
			
			
			Date dNow = new Date(); // 当前时间
			Date dBefore = new Date();
			Date dAfter = new Date();

			Calendar calendar1 = Calendar.getInstance(); // 得到日历
			calendar1.setTime(dNow);// 把当前时间赋给日历
			calendar1.add(Calendar.DAY_OF_MONTH, -1); // 
			dBefore = calendar1.getTime(); // 得到前一天的时间

			Calendar calendar2 = Calendar.getInstance(); // 得到日历
			calendar2.setTime(dNow);// 把当前时间赋给日历
			calendar2.add(Calendar.DAY_OF_MONTH, 99999); // 
			dAfter = calendar2.getTime(); // 得到前一天的时间

			sellercreatebatchBean.setEffectiveStartDate(dBefore);
			sellercreatebatchBean.setEffectiveEndDate(dAfter);
		
			SellerCreateBatchCall sellerCreateBatchCall = CouponShop2cnMCallService.SellerCreateBatch(sellercreatebatchBean);
		
			getbatchinfoBean.setBatchCode(sellerCreateBatchCall.getBatchCode());
			getbatchinfoCall.setData(getbatchinfoBean);
			getbatchinfoCall.callService();			
			
//			 Logger.verifyEquals((byte)2, getbatchinfoCall.getBatchInfo().getCouponType(),
//		                "验证返回coupontype");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_GetBatchInfo_007() {
		Logger.start(true,"获取批次信息-商家优惠券-活动优惠券,目前神器不存在活动");
		try {
			// 活动优惠券
			CouponShop2cnMCallService CouponShop2cnMCallService = new CouponShop2cnMCallService();
			sellercreatebatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatch_ActivityBean();
		
			SellerCreateBatchCall sellerCreateBatchCall = CouponShop2cnMCallService.SellerCreateBatch(sellercreatebatchBean);
			List<couponbatch> list = new couponbatchWapper().selectBybatchCode(sellerCreateBatchCall.getBatchCode());
			
			getbatchinfoBean.setBatchCode(sellerCreateBatchCall.getBatchCode());
			getbatchinfoCall.setData(getbatchinfoBean);
			getbatchinfoCall.callService();			
			
			VerifyReturn(list.get(0));
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	
	private void VerifyReturn(couponbatch item) {
		
        BatchInfo batchInfo = getbatchinfoCall.getBatchInfo();
		checkCouponBatch(item,batchInfo);
        //验证使用条件
        checkcouponusecondition(item.getBatchId(),batchInfo);

    }
	private void checkCouponBatch(couponbatch item,BatchInfo batchInfo)
	{
		 Logger.comment("====方法：验证数据库checkcouponbatch====");
	        Logger.verifyEquals("0", getbatchinfoCall.getCode(),
	                "验证返回code");
	     
	        Logger.verifyEquals("操作成功！",
	                getbatchinfoCall.getMessage(), "验证返回Message");


	        String batchCode = getbatchinfoBean.getBatchCode();

	        Logger.verifyEquals(item.getBatchId(),
	        		batchInfo.getBatchId(),
	                "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
	                        + "记录中iBatchId字段相同");
	        
	        Logger.verifyEquals(item.getUseConditionType(), batchInfo.getUseConditionType(),
					"验证返回字段productLimitType与Ymt_CouponSetting表中UseConditionType一致");
	        
	        Logger.verifyEquals(item.getBatchCode(),
	        		batchInfo.getBatchCode(),
	                "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
	                        + "记录中sBatchCode字段相同");
	        
	        // 验证AcquireStartTime，AcquireEndTime，AcquireCouponMethod
	        SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:ms"); 
	        if(item.getAcquireStartTime()!=null&&batchInfo.getAcquireStartTime()!=null)
	        {
	        	String acquireStartTime =time.format(item.getAcquireStartTime());
	        	Logger.verifyEquals(acquireStartTime.substring(0, 19).replace(" ", "T"),
	        		batchInfo.getAcquireStartTime(),
	                "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
	                        + "记录中AcquireStartTime字段相同");
	        }
	        if(item.getAcquireEndTime()!=null&&batchInfo.getAcquireEndTime()!=null)
	        {
	        	String acquireEndTime =time.format(item.getAcquireEndTime());
	        	Logger.verifyEquals(acquireEndTime.substring(0, 19).replace(" ", "T"),
	        		batchInfo.getAcquireEndTime(),
	                "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
	                        + "记录中AcquireEndTime字段相同");
	        }
	        // 是否作废
	        if(item.getIsInvalid()==null)
	        {
	        	Logger.verifyEquals(false,
	            		batchInfo.getIsExpire(),
	                    "验证返回IsCancellation与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
	                            + "记录中bInvalidStatus字段相同");
	        }
	        else {
	        	Logger.verifyEquals(item.getIsInvalid(),
	            		batchInfo.getIsExpire(),
	                    "验证返回IsCancellation与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
	                            + "记录中IsInvalid字段相同");
			}
	        
	     // 是否领完
	        Logger.verifyEquals(item.getTotalNum()==item.getReceivedCount(),
	        		batchInfo.getReceiveOver(),
	                "验证返回IsReceiveOver正确");
	           
	   
	        Logger.verifyEquals(item.getSendType(),
	        		batchInfo.getSendType(),
	                "验证返回SendType与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
	                        + "记录中iSendType字段相同");
	        
	        if(item.getEffectiveType()==null)
	        {
	        	Logger.verifyEquals(0,
	        		batchInfo.getEffectiveType(),
	                "验证返回EffectiveType与数据库中表CouponBatch中iEffectiveType字段相同");
	        }
	        else{
	        	   Logger.verifyEquals(item.getEffectiveType(),
	               		batchInfo.getEffectiveType(),
	                       "验证返回EffectiveType与数据库中表CouponBatch中iEffectiveType字段相同");
	        }
	        
	        if(item.getEffectiveValidType()==null)
	        {
	        	 Logger.verifyEquals(0,
	             		batchInfo.getEffectiveValidType(),
	                     "验证返回EffectiveValidType与数据库中表CouponBatch中iEffectiveValidType字段相同");
	             }
	        else
	        {
	        	 Logger.verifyEquals(item.getEffectiveValidType(),
	             		batchInfo.getEffectiveValidType(),
	                     "验证返回EffectiveValidType与数据库中表Ymt_CouponSetting中iEffectiveValidType字段相同");
	        
	        }

	        Logger.verifyEquals(time.format(item.getValidStart()).subSequence(0, 19),
	        		batchInfo.getEffectiveStartDate().replace("T", " "),
	                "验证返回ValidStart与数据库中表couponbatch中ValidStart字段相同");

	        Logger.verifyEquals(time.format(item.getValidEnd()).subSequence(0, 19),
	        		batchInfo.getEffectiveEndDate().replace("T", " "),
	                "验证返回ValidEnd与数据库中表couponbatch中ValidEnd字段相同");
	        
	        if(item.getEffectiveDays()==null)
	        {
	        	Logger.verifyEquals(0,
	        		batchInfo.getEffectiveDays(),
	                "验证返回iEffectiveDays与数据库中表couponbatch中iEffectiveDays字段相同");
	        }
	        else {
	        	Logger.verifyEquals(item.getEffectiveDays().intValue(),
	            		batchInfo.getEffectiveDays(),
	                    "验证返回iEffectiveDays与数据库中表couponbatch中iEffectiveDays字段相同");     
			}
	        
	        Logger.verifyEquals(item.getTotalNum(),
	        		batchInfo.getCouponTotalCount(),
	                "验证返回TotalCount与数据库中表couponbatch中iCouponTotalNum字段相同");
	        
	        Logger.verifyEquals(item.getReceivedCount(),
	        		batchInfo.getReceivedCount(),
	                "验证返回BatchCode与数据库中表couponbatch中ReceivedCount字段相同");
	        
	        if(item.getIsShowInPage()==null)
	        {
	        	Logger.verifyEquals(false,
	        		batchInfo.getIsShowInPage(),
	                "验证返回BatchCode与数据库中表couponbatch中sBatchCode=" + batchCode
	                        + "记录中IsShowInPage字段相同");
	        }
	        else {
	            Logger.verifyEquals(item.getIsShowInPage(),
	            		batchInfo.getIsShowInPage(),
	                    "验证返回BatchCode与数据库中表couponbatch中sBatchCode=" + batchCode
	                            + "记录中IsShowInPage字段相同");
			}
	        
	        Logger.verifyEquals(item.getReceiveUserType(),
	        		batchInfo.getUserType(),
	                "验证返回BatchCode与数据库中表couponbatch中iUserType字段相同");
	        // 验证coupontype-分享券
	     //   int couponType=item.getAcquireMethod()==3?2:1;
	        Logger.verifyEquals((byte)item.getCouponType(),
	        		batchInfo.getCouponType(),
	                 "验证返回BatchCode与数据库中表Ymt_CouponBatch中coupontype字段相同");
	        
	        //活动Id
	        Logger.verifyEquals(item.getCouponTypeBizValue(),
            		batchInfo.getCouponTypeBizValue(),
                    "验证返回BatchCode与数据库中表couponbatch中sBatchCode=" + batchCode
                            + "记录中CouponTypeBizValue字段相同");
	        
	        Logger.verifyEquals(item.getEffectiveTimeType(), batchInfo.getEffectiveTimeType(), 
	        		"验证返回EffectiveTimeType与数据库中couponbatch中sBatchCode=" + batchCode
	        		+"记录中EffectiveTimeType字段相同");
	        
	        
	        
	        Logger.verifyEquals(item.getReceiveTimesPerUser(), batchInfo.getReceiveNumPerUser(),
						"验证返回字段ReceiveNumPerUser与couponbatch表中ReceiveNumPerUser一致");
	        
	        //币种
	        Logger.verifyEquals(item.getCurrency(), batchInfo.getCurrency(), 
	        		"验证返回Currency与数据库中couponbatch中sBatchCode=" + batchCode
	        		+"记录中Currency字段相同");
	}
	/**
	 * 
	 * @param batchId
	 * @param item 数据库查询couponbatch
	 * @param batchInfo call返回的batchInfo
	 */
	 private void checkcouponusecondition(int batchId,BatchInfo callbatchInfo)
	    {
	    	 Logger.comment("====方法：验证数据库checkcouponusecondition====");
	    	 Logger.comment("########## couponusecondition 限制条件表 #############");
				couponuseconditionWapper userWapper=new couponuseconditionWapper();
				List<couponusecondition> userLst=userWapper.selectByBatchId(batchId);
				
				couponbatch couponbatch= new couponbatchWapper().selectByPrimaryKey(batchId);
				
				if(callbatchInfo.getUseConditionType()==1)
				{
					Logger.verifyEquals(1, userLst.size(), "验证数据库couponusecondition表中的BatchId="+batchId+"的记录中的条数");
					Logger.verifyEquals(1,userLst.get(0).getConditionType(),
							"验证数据库couponusecondition表中BatchId="+ batchId+ "的记录中的ConditionValue()");
					Logger.verifyEquals(couponbatch.getOperatorId().toString(),userLst.get(0).getConditionValue(),
							"验证数据库couponusecondition表中BatchId="+ batchId+ "的记录中的ConditionValue()");
				}	
				else if(callbatchInfo.getUseConditionType()==2)
				{
					Logger.verifyEquals(true, userLst.size()>1, "验证数据库couponusecondition表中的BatchId="+batchId+"的记录中的条数最少2条");
					for(int i=0;i<userLst.size();i++)
					{
						if(userLst.get(i).getConditionType()==1)
						Logger.verifyEquals(couponbatch.getOperatorId().toString(),userLst.get(i).getConditionValue(),
								"验证数据库couponusecondition表中BatchId="+ batchId+ "的记录中的ConditionValue()");
						else if(userLst.get(i).getConditionType()==2)
						{
							if(sellercreatebatchBean.getproductIdList()!=null && sellercreatebatchBean.getproductIdList().size()>0)
							Logger.verifyEquals(true,sellercreatebatchBean.getproductIdList().contains(userLst.get(i).getConditionValue()),
									"验证数据库couponusecondition表中BatchId="+ batchId+ "的记录中的ConditionValue()");
						}
					}
				}
	    }
}
